var _ = require('lodash')
import ConfigUtil from '@/utils/graph-utils/configUtil'

export default {
  getNodeAttr(node, attrName) {
    const attrPath = 'body/' + attrName
    const attr = node.getAttrByPath(attrPath)
    if (attr) {
      return attr
    }
    const attrs = node.getAttrs()
    if (node.shape === 'rect') {
      return attrs.rect[attrName]
    } else if (node.shape === 'circle') {
      return attrs.circle[attrName]
    } else if (node.shape === 'ellipse') {
      return attrs.ellipse[attrName]
    } else if (node.shape === 'polygon') {
      return attrs.polygon[attrName]
    } else if (node.shape === 'polyline') {
      return attrs.polyline[attrName]
    } else if (node.shape === 'text-block') {
      return attrs.label[attrName]
    } else {
      // 循环节点的所有属性，查找对应的属性值
      const keys = Object.keys(attrs)
      for (const key of keys) {
        const attrItem = attrs[key]
        const itemKeys = Object.keys(attrItem)
        for (const itemKey of itemKeys) {
          if (itemKey === attrName) {
            return attrItem[itemKey]
          }
        }
      }
    }
  },

  getFillAttr(node) {
    return this.getNodeAttr(node, 'fill')
  },

  getStrokeAttr(node) {
    return this.getNodeAttr(node, 'stroke')
  },

  getStrokeWidthAttr(node) {
    return this.getNodeAttr(node, 'strokeWidth')
  },

  getPointsAttr(node) {
    return this.getNodeAttr(node, 'points')
  },

  getTextAttr(node) {
    return this.getNodeAttr(node, 'text')
  },

  setNodeAttrs(node, form) {
    const orginShpaes = ConfigUtil.getX6Shapes()
    // 只允许修改原生节点的属性，自定义节点不允许修改
    if (orginShpaes.indexOf(node.shape) === -1 && node.shape !== 'simpleText') {
      return
    }

    const attr = {
      body: {}
    }
    if (form.fill) {
      attr.body.fill = form.fill
    }
    if (form.fillNone) {
      attr.body.fill = 'none'
    }
    if (form.stroke) {
      attr.body.stroke = form.stroke
    }
    if (form.strokeNone) {
      attr.body.stroke = 'none'
    }
    if (form.strokeWidth) {
      attr.body.strokeWidth = form.strokeWidth
    }
    if (form.points) {
      attr.body.refPoints = form.points
    }
    if (form.text) {
      attr.label = {
        text: form.text
      }
      attr.body.text = form.text
    }
    node.setAttrs(attr)
  },

  setNodeAttr(node, attrName, attrValue) {
    const newStyleValue = attrName + ':' + attrValue + ';'
    const attrs = _.cloneDeep(node.getAttrs())
    if (node.isEdge()) {
      const value = attrs['line']
      let style = _.get(value, 'style', '')
      if (style === '') {
        _.set(value, 'style', newStyleValue)
      } else {
        if (style.indexOf(attrName) === -1) {
          style += newStyleValue
        } else {
          const pattern = new RegExp(attrName + ':([^:]+)?;', 'ig')
          style = _.replace(style, pattern, newStyleValue)
        }
        _.set(value, 'style', style)
      }
    } else {
      _.forIn(attrs, function(value, key) {
        if (attrName === 'style' || ConfigUtil.isStyleAttr(attrName)) {
          let style = _.get(value, 'style', '')
          if (style === '') {
            _.set(value, 'style', newStyleValue)
          } else {
            if (style.indexOf(attrName) === -1) {
              style += newStyleValue
            } else {
              const pattern = new RegExp(attrName + ':([^:]+)?;', 'ig')
              style = _.replace(style, pattern, newStyleValue)
            }
            _.set(value, 'style', style)
          }
        } else {
          _.set(value, attrName, attrValue)
        }
      })
    }
    node.setAttrs(attrs)
  }
}
